﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;

namespace YWTSSys.Library.Extention
{
    /// <summary>
    /// 提供一些RadioButtonList对象常用的操作方法
    /// </summary>
    public static class RadioButtonListExt
    {
        #region 获取RadioButtonList控件中选中项的值
        /// <summary>
        /// 获取选定项的Value属性值
        /// </summary>
        /// <typeparam name="T">要转换的数据类型</typeparam>
        /// <param name="rbl">RadioButtonList对象</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>返回选定项的Value属性值</returns>
        public static T GetSelecedValue<T>(this RadioButtonList rbl, T defaultValue)
        {
            if (rbl.SelectedItem != null)
            {
                return rbl.SelectedItem.Value.ConvertToType<T>(defaultValue);
            }
            else
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// 获取选定项的Text属性值
        /// </summary>
        /// <typeparam name="T">要转换的数据类型</typeparam>
        /// <param name="rbl">RadioButtonList对象</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>返回选定项的Value属性值</returns>
        public static T GetSelectedText<T>(this RadioButtonList rbl, T defaultValue)
        {
            if (rbl.SelectedItem != null)
            {
                return rbl.SelectedItem.Text.ConvertToType<T>(defaultValue);
            }
            else
            {
                return defaultValue;
            }
        }
        #endregion

        #region 根据指定值选择RadioButtonList控件中的对应项
        /// <summary>
        /// 根据Value找选择项
        /// </summary>
        /// <param name="rbl">RadioButtonList控件</param>
        /// <param name="val">指定的Value属性值</param>
        public static void SelectByValue(this RadioButtonList rbl, string val)
        {
            if (string.IsNullOrEmpty(val))
            {
                return;
            }
            if (rbl.Items != null && rbl.Items.Count > 0)
            {
                foreach (ListItem item in rbl.Items)
                {
                    item.Selected = false;
                }
            }
            ListItem li = rbl.Items.FindByValue(val);
            if (li != null)
            {
                li.Selected = true;
            }
        }

        /// <summary>
        /// 根据Text找选择项
        /// </summary>
        /// <param name="rbl">RadioButtonList控件</param>
        /// <param name="textVal">指定的Text属性值</param>
        public static void SelectedByText(this RadioButtonList rbl, string textVal)
        {
            if (string.IsNullOrEmpty(textVal))
            {
                return;
            }
            if (rbl.Items != null && rbl.Items.Count > 0)
            {
                foreach (ListItem item in rbl.Items)
                {
                    item.Selected = false;
                }
            }
            ListItem li = rbl.Items.FindByText(textVal);
            if (li != null)
            {
                li.Selected = true;
            }
        }
        #endregion
    }
}
